/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Karyawan;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author MiChi
 */
public class KaryawanDAOImpl extends GeneralDAOImpl implements KaryawanDAO {

    public KaryawanDAOImpl(EntityManager em) {
        super(em);
    }

    public Karyawan get(Long id) throws Exception {
        Karyawan k = null;

        try {
            em.getTransaction().begin();
            k = em.find(Karyawan.class, id);
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }

        return k;
    }

    public List<Karyawan> gets() throws Exception {
        List<Karyawan> list = new ArrayList<Karyawan>();

        try {
            em.getTransaction().begin();
            list = em.createQuery("SELECT k FROM Karyawan k").getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }

        return list;
    }

    public List<Karyawan> findNama(String nama) throws Exception {
        nama = "%" + nama + "%";
        List<Karyawan> list = new ArrayList<Karyawan>();
        try {
            em.getTransaction().begin();
            list = em.createQuery("SELECT k FROM Karyawan k WHERE k.nama LIKE :n").setParameter("n", nama).getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }
        return list;
    }
}
